package com.zd.web.servlet;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zd.entity.Novel;
import com.zd.service.NovelService;
import com.zd.service.impl.NovelServiceImpl;

/**
 * Servlet implementation class AddNovelServlet
 */
@WebServlet("/AddNovelServlet")
public class AddNovelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	NovelService nl=new NovelServiceImpl();
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("进入添加小说");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String novelName=request.getParameter("novelName");
//		int novelId=Integer.parseInt(request.getParameter("novelId"));
		String writerName=request.getParameter("writerName");
		String novelImgURL=request.getParameter("novelImgURL");
		String novelType1=request.getParameter("novelType1");
		String novelIntroduction=request.getParameter("novelIntroduction");
		int hits=Integer.parseInt(request.getParameter("hits"));
		
//		String releaseTime=request.getParameter("releaseTime");
//	    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        LocalDate localDate = LocalDate.parse(releaseTime, formatter);
        // 如果你需要java.util.Date对象
//        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
		
		LocalDateTime localDateTime = LocalDateTime.now(); // 获取当前日期和时间
        Date currentDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); // 转换为Date对象
        System.out.println("当前时间：" + currentDate);
		
	    Novel novel= new Novel();
	    novel.setHits(hits);
//	    novel.setNovelId(novelId);
	    novel.setNovelImgURL(novelImgURL);
	    novel.setNovelIntroduction(novelIntroduction);
	    novel.setReleaseTime(currentDate);
	    novel.setNovelName(novelName);
	    //调用service判断输入类型是否 存在  如果不存在就先添加再获取（进入service判断返回添加或找到的类型id）
	    int novelTypeId=nl.selectTypeis(novelType1);
	    //上面返回了类型id
	    //判断作者是否存在 如果不存在就先添加再获取（进入service判断返回添加或找到的作者id）
	    int writerId=nl.selectWriteris(writerName);
	    System.out.println("类型Id"+novelTypeId+"writerId:"+writerId);
	    //执行添加
		int result=nl.addNovel(novel,novelTypeId,writerId);
		response.sendRedirect("novel.jsp");
	}

}
